package tichu_project252.Round;

import java.util.Collections;
import java.util.LinkedList;
import tichu_project252.CardClasses.Card;
import tichu_project252.Table;

public class Street extends TurnType {

    /**
     * Returns true if the combination given is street.
     *
     * <p> <b> Accessor(Selector): </b> isStreet(LinkedList <Card> l, Table t) </p>
     *
     * <p><b> Precondition: </b> The player should select some cards.</p> </p>
     * <p> <b> Postcondition:</b> Returns true if the combination is street,
     * else it returns false. </p>
     *
     * @return if the combination is street.
     */
    public static boolean isStreet(LinkedList<Card> l, Table t, boolean bomb) {
        
        if (l.size() < 5) { //the street must have 5 or more cards
            return false;
        }
        
        LinkedList<Integer> temp = new LinkedList<Integer>();
        Collections.sort(l, new CardComparator());

        
        if (hasPhoenix(l)) { //check if there is a phoenix
            int i;
            //find the spot to put the phoenix
            for(i = 1; i < l.size() - 1; i++) {
                //if we've got a position where two cards in a row have different 
                //intReps, then we must insert the phoenix card between them
                if(l.get(i).getintRepr() - l.get(i-1).getintRepr() != 1) {
                    break;
                }
            }
            //the last card is usually the phoenix card, as it has the higher intRepr by default
            l.get(l.size()-1).setintRepr(l.get(i-1).getintRepr()+1);
            Collections.sort(l, new CardComparator());
        }

        for (int i = 0; i < l.size(); i++) {
            temp.add(l.get(i).getintRepr());
        }
        Collections.sort(temp);
        temp = removeDuplicates(temp);
        
        //if l does not have all-different cards
        if (l.size() != temp.size()) {
            return false;
        }

        if (t.getLastDropped().size() == 0) {
            return true;
        } else if (bomb) {
            return true;
        } else if (l.size() != t.getLastDropped().size()) {
            return false;
        } else if (t.getLastDropped().getFirst().getintRepr() < l.getFirst().getintRepr()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Check if we have flush in our hand.
     * 
     * 
     * @param l The selected cards
     * @param t The table
     * @return If the combination is flush (bomb) or not
     */
    public static boolean isFlush(LinkedList<Card> l, Table t, boolean bomb) {
        boolean flag = false;
        String suit = l.get(0).getSuit();

        for (int i = 0; i < l.size(); i++) {
            if (l.get(i).getstringRepr().equals("Phoenix")) {
                return false;
            }

            if (l.get(i).getSuit().equals(suit)) {
                flag = true;
            } else {
                return false;
            }
        }

        return isStreet(l, t, bomb) && flag;
    }

}
